<template>
  <div style="padding: 10px;">
    <section>
      <h3>输入项目</h3>
      <el-select
          type="text" v-model="queryParams.projectCode" placeholder="projectCode"
          @change="()=>triggerinitcoms()"
      >
        <el-option value="system" name="system"  label="system"></el-option>
        <el-option value="erp" name="erp"  label="erp"></el-option>
        <el-option value="mes" name="mes"  label="mes"></el-option>
        <el-option value="biapi" name="biapi"  label="biapi"></el-option>
        <el-option value="橙小会" name="橙小会"  label="橙小会"></el-option>
      </el-select>
    </section>
    <div>
      <h3>导入swagger</h3>
      <el-input type="text" v-model="apidosurl" aria-placeholder="输入 swaggerui的路径" />
      <button
      :loading="loadingexport"
      @click="() => {
                导入swagger()
            }">导入swagger
      </button>
    </div>
    <div>

      <el-tabs v-model="activeName" @tab-click="changeTab">
        <el-tab-pane name="api" label="api"></el-tab-pane>
        <el-tab-pane name="sechma" label="sechma"></el-tab-pane>
      </el-tabs>


      <apiitemCom ref="apiitemref" v-show="activeName=='api'"></apiitemCom>
      <schemaslistCom ref="schemaslistref" v-show="activeName=='sechma'"></schemaslistCom>

    </div>

  </div>
</template>
<script>
import axios from 'axios';
import { alertWithPayload } from '../../../../lib/utils/dist/utils';
import schemaslistCom from './schemaslist.vue';
import apiitemCom from './apiitem.vue';

export default {
  data() {
    var projectCode = sessionStorage.getItem('queryParams.projectCode');
    var apidosurl = sessionStorage.getItem('queryParams.apidosurl');
    return {
      loadingexport: false,
      activeName: 'api',
      total: 0,
      dataList: [],
      queryParams: {
        projectCode: projectCode,
        name: '',
        pageSize: 5,
      },
      apidosurl: apidosurl,
    };
  },

  components: {
    apiitemCom,
    schemaslistCom,
  },
  mounted() {
    this.changeTab();
  },
  methods: {
    changeTab() {
      setTimeout(() => {
        if (this.activeName == 'api') {
          this.$refs['apiitemref'].initByDialog({
            projectCode: this.queryParams?.projectCode,
          });
        } else if (activeName == 'sechma') {
          this.$refs['apiitemref'].initByDialog({
            projectCode: this.queryParams?.projectCode,
          });
        }
      },200);
    },

    triggerinitcoms() {
      sessionStorage.setItem('queryParams.projectCode', this.queryParams?.projectCode || '');
      this.$refs['apiitemref'].initByDialog({
        projectCode: this.queryParams?.projectCode,
      });

      this.$refs['schemaslistref'].initByDialog({
        projectCode: this.queryParams?.projectCode,
      });
    },
    initByDialog(_data) {
      this.queryParams.projectCode = _data?.projectCode;
    },
    async 导入swagger() {
    this.loadingexport=true
      sessionStorage.setItem('queryParams.apidosurl', this.apidosurl || '');


      var result = await axios.post('/apis/swagger1/importswaggerui', {
        apidosurl: this.apidosurl,
        projectCode: this.queryParams.projectCode,
      });
      alertWithPayload(result);
      this.loadingexport=false
    },

  },
};</script>
